package com.bestsonic.crawler.model;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex implements Regular<String>{
	private String regex;
	private int index;
	private String prefix;
	private String suffix;
	
	public Regex() {
		super();
	}
	
	
	
	public Regex(String regex, int index) {
		super();
		this.regex = regex;
		this.index = index;
		this.prefix = "";
		this.suffix = "";
	}



	public Regex(String regex, int index, String prefix, String suffix) {
		super();
		this.regex = regex;
		this.index = index;
		this.prefix = prefix;
		this.suffix = suffix;
	}

	public String getRegex() {
		return regex;
	}
	public void setRegex(String content) {
		this.regex = content;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	
	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public List<String> matches(String content, int flags) {
		List<String> list = new ArrayList<String>();
		Pattern pattern = Pattern.compile(this.getRegex(), flags);
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()){
			if(this.getPrefix() == null)
				this.setPrefix("");
			if(this.getSuffix() == null)
				this.setSuffix("");
			list.add(this.getPrefix() + matcher.group(this.getIndex()) + this.getSuffix());
		}
		return list;
	}
}
